package com.codefox.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.codefox.entity.TbDoctor;
import com.codefox.service.TbDoctorService;
import com.codefox.util.PageUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 医生控制层
 *
 * @author xiaobiye
 * @date 2018年9月28日21:33:49
 */
@Controller
@RequestMapping(value={"front", "back"})
public class TbDoctorController {
	@Autowired
    TbDoctorService doctorService;
	
	/**
	 * 返回所有医生的json
	 * @return json字符串
	 */
	@ResponseBody
    @RequestMapping(value="/doctorList",produces = "application/json;charset=UTF-8")
    public String list() {
        List<TbDoctor> list = doctorService.selectAll();
        String json = JSONObject.toJSONString(list);
        return json;
    }
	
	@ResponseBody
    @RequestMapping(value="/findDoctorById",produces = "application/json;charset=UTF-8")
	public String findById(int id){
		TbDoctor tbDoctor = doctorService.findById(id);
		String json = JSONObject.toJSONString(tbDoctor);
		return json;
	}
	@ResponseBody
    @RequestMapping(value="/doctorDelete",produces = "application/json;charset=UTF-8")
    public String delete(@RequestParam("id")Integer id ) {
		if (doctorService.delete(id)>0) {
			return "Y";
		}
		return "N";
    }
	
	@ResponseBody
    @RequestMapping(value="/doctorEdit",produces = "application/json;charset=UTF-8")
    public ModelAndView update(TbDoctor tbDoctor) {
        doctorService.update(tbDoctor);
        return new ModelAndView("redirect:doctorInfo.html?#");
    }
    
    @ResponseBody
    @RequestMapping(value="/doctorAdd",produces = "application/json;charset=UTF-8")
    public String add(TbDoctor doctor) {
    	 if (doctorService.add(doctor)>0) {
 			return "Y";
 		}
         return "N";
    }

    @ResponseBody
    @RequestMapping(value="/getDoctorPage",produces = "application/json;charset=UTF-8")
    public String getDoctorPage(@RequestParam("pageIndex")int pageIndex,@RequestParam("pageSize")int pageSize){
    	PageUtil<TbDoctor> pageUtil = doctorService.getDoctorPage(pageIndex, pageSize);
    	return JSON.toJSONString(pageUtil);
    }

}
